--[[
	Galactica
]]

Galactica.Props = {}

Galactica.Undo = {}
Galactica.Undo.Create = undo.Create
Galactica.Undo.AddEntity = undo.AddEntity
Galactica.Undo.SetPlayer = undo.SetPlayer
Galactica.Undo.Finish = undo.Finish

function GM:SetPropOwner(ply, ent)
	table.insert(Galactica.Props, {ply:SteamID(), ent})
	ent:SetNetworkedString("Galactica.Owner", ply:Nick())
end

function GM:CheckPropOwner(ply, ent)
	if(ply:IsAdmin()) then return true end
	for k, v in pairs(Galactica.Props) do
		if(v[1] == ply:SteamID() and v[2] == ent) then
			return true
		end
	end
	return false
end

function undo.Create(name)
    Galactica.Undo.Current = {}
	Galactica.Undo.Current.Player = false
    Galactica.Undo.Current.Elements = {}
	
    Galactica.Undo.Create(name)
end

function undo.AddEntity(ent)
    if(Galactica.Undo.Current and ent and ent:IsValid()) then
        table.insert(Galactica.Undo.Current.Elements, ent)
    end
	
    Galactica.Undo.AddEntity(ent)
end

function undo.SetPlayer(ply)    
    if(Galactica.Undo.Current and ply and ply:IsValid()) then
        Galactica.Undo.Current.Player = ply
    end
    
    Galactica.Undo.SetPlayer(ply)
end

function undo.Finish()
    if(Galactica.Undo.Current and Galactica.Undo.Current.Player != false and Galactica.Undo.Current.Elements) then 
        for k, ent in pairs(Galactica.Undo.Current.Elements) do
			self:SetPropOwner(Galactica.Undo.Current.Player, ent)
        end
    end
    
    Galactica.Undo.Finish()
end

function GM:PropRemoval(SteamID, PlayerName)
	for k, v in pairs(Galactica.Props) do
		if(v[1] == ply:SteamID() and v[2]:IsValid()) then
			v[2]:Remove()
		end
	end
	self:NofityAll(tostring(PlayerName).."'s props have been cleaned up")
end

function GM:PlayerInitialSpawn(ply)
	local TimerName = "Galactica.PropRemoval: "..ply:SteamID()
	if(timer.IsTimer(TimerName)) then
		timer.Remove(TimerName)
	end
end

function GM:PlayerDisconnected(ply)
	timer.Create("Galactica.PropRemoval: "..ply:SteamID(), Galactica.PropRemovalDelay, 1, self.PropRemoval, self, ply:SteamID(), ply:Nick())
end

function GM:CanTool(ply, tr, mode)
	if(tr.HitWorld) then return end
	ent = tr.Entity
	if(!ent or !ent:IsValid() or ent:IsPlayer()) then return false end
	if(!self:CheckPropOwner(ply, ent)) then
		return false
	elseif(toolgun == "nail") then
		local Trace = {}
		Trace.start = tr.HitPos
		Trace.endpos = tr.HitPos + (ply:GetAimVector() * 16.0)
		Trace.filter = {ply, tr.Entity}
		local tr2 = util.TraceLine(Trace)
		if(tr2.Hit && !tr2.Entity:IsPlayer()) then
			if(!self:CheckPropOwner(ply, tr2.Entity)) then
				return false
			end
		end
	end	
end

function GM:GravGunPunt(ply, ent)
	if(!ent:IsValid()) then return end
	if(!self:CheckPropOwner(ply, ent)) then
		return false
	end
end

function GM:GravGunPickupAllowed(ply, ent)
	if(!ent:IsValid()) then return end
	if(!self:CheckPropOwner(ply, ent)) then
		return false
	end
end

function GM:PhysgunPickup(ply, ent)
	if(!ent:IsValid()) then return end
	if(!self:CheckPropOwner(ply, ent)) then
		return false
	end
end

function GM:OnPhysgunReload(weapon, ply)
	local tr = util.TraceLine(util.GetPlayerTrace(ply))
	if(!tr.HitNonWorld || !tr.Entity:IsValid() || tr.Entity:IsPlayer()) then return end
	if(!self:CheckPropOwner(ply, tr.Entity)) then
		return false
	end
end
